package com.nexgo.payment.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.CellIdentityCdma;
import android.telephony.CellIdentityGsm;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityWcdma;
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static android.telephony.TelephonyManager.SIM_STATE_READY;

/**
 * Created by wzp on 2018/2/7.
 */

public class BassInfoUtil {
    private static final String BASEINFO = "base_info";
    private ConnectivityManager connectManager = null;
    private NetworkInfo mobNetInfo = null;
    private Context mContext = null;
    private static SPUtil mSPUtil ;
    private static  Logger mLog = LoggerFactory.getLogger(BassInfoUtil.class.getSimpleName());
    public BassInfoUtil(Context context, String baseInfo) {
        this.mContext = context;
        mSPUtil = SPUtil.getInstance(context,baseInfo);
    }

    public boolean bisNetConnected(){
        if(mobNetInfo != null){
            return mobNetInfo.isConnected();
        }else{
            return false;
        }
    }



    public  String getBaseInfo(){
        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        if(tm == null){
            return null;
        }

        int type = tm.getNetworkType();//获取网络类型
        StringBuilder bytes = new StringBuilder();
        //中国电信为CTC
        //NETWORK_TYPE_EVDO_A是中国电信3G的getNetworkType
        //NETWORK_TYPE_CDMA电信2G是CDMA
        //NETWORK_TYPE_EVDO_0
        if (type == TelephonyManager.NETWORK_TYPE_EVDO_A || type == TelephonyManager.NETWORK_TYPE_EVDO_0
                || type == TelephonyManager.NETWORK_TYPE_CDMA || type == TelephonyManager.NETWORK_TYPE_1xRTT) {
            CdmaCellLocation location = (CdmaCellLocation) tm.getCellLocation();
            if (location == null)
                return null;
            String operator = tm.getNetworkOperator();
            int cellIDs = location.getBaseStationId();
            int networkID = location.getNetworkId();
            StringBuilder sb = new StringBuilder();
            sb.append(location.getSystemId());
            int mnc = Integer.parseInt(sb.toString());
            int mcc = Integer.parseInt(operator.substring(0, 3));

            Log.d("getBaseInfo", " MCC = " + mcc + "\t MNC = " + mnc + "\t networkID = " + networkID + "\t CID = " + cellIDs);

            bytes.append(mcc);
            bytes.append(mnc);
            bytes.append(cellIDs);
            bytes.append(networkID);
        }
        //移动2G卡 + CMCC + 2
        //type = NETWORK_TYPE_EDGE
        //联通的2G经过测试 China Unicom 1 NETWORK_TYPE_GPRS
        else if (type == TelephonyManager.NETWORK_TYPE_EDGE || type == TelephonyManager.NETWORK_TYPE_GPRS
                || type == TelephonyManager.NETWORK_TYPE_HSDPA) {
            GsmCellLocation location = (GsmCellLocation) tm.getCellLocation();
            if (location == null) {
                return null;
            }
            String operator = tm.getNetworkOperator();
            int mcc = Integer.parseInt(operator.substring(0, 3));
            int mnc = Integer.parseInt(operator.substring(3));
            int cid = location.getCid();
            int lac = location.getLac();
            Log.d("getBaseInfo", " MCC = " + mcc + "\t MNC = " + mnc + "\t lac = " + lac + "\t CID = " + cid);
            bytes.append(mcc);
            bytes.append(mnc);
            bytes.append(lac);
            bytes.append(cid);
        }
        Log.d("getBaseInfo", " bytes = " + bytes.toString());
        return bytes.toString();
    }

    //初始化
    public void initAllBaseModeInfo(){
        List<CellInfo> infoLists;
        List<String> baseInfoList ;
        StringBuilder bytes = new StringBuilder();
        TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        if(telephonyManager != null && telephonyManager.getSimState() == SIM_STATE_READY){
            Log.d("getAllBaseModeInfo","telephonyManager != null");
            infoLists = telephonyManager.getAllCellInfo();
            if(infoLists != null && infoLists.size() > 0){
                {
                    baseInfoList = new ArrayList<>();
                    for (int i = 0 ; i < infoLists.size() ; i++){
                        CellInfo cellInfo = infoLists.get(i);{
                            if( cellInfo instanceof CellInfoGsm){
                                CellInfoGsm cellInfoGsm = (CellInfoGsm)cellInfo;
                                CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();

                                bytes.append("MCC:");
                                bytes.append(cellIdentityGsm.getMcc());
                                bytes.append("|");

                                bytes.append("MNC:");
                                bytes.append(cellIdentityGsm.getMnc());
                                bytes.append("|");

                                bytes.append("CID:");
                                bytes.append(cellIdentityGsm.getCid());
                                bytes.append("|");

                                bytes.append("LAC:");
                                bytes.append(cellIdentityGsm.getLac());
                                bytes.append("|");
                                baseInfoList.add(bytes.toString());
                            }else if(cellInfo instanceof CellInfoCdma){  //CMDA电信
                                CellInfoCdma cellInfoCdma = (CellInfoCdma)cellInfo;
                                CellIdentityCdma cellIdentityCdma= cellInfoCdma.getCellIdentity();

                                bytes.append("MCC:");
                                bytes.append(telephonyManager.getNetworkOperator().substring(0, 3));
                                bytes.append("|");

                                bytes.append("MNC:");
                                bytes.append(cellIdentityCdma.getSystemId());
                                bytes.append("|");

                                bytes.append("CID:");
                                bytes.append(cellIdentityCdma.getBasestationId());
                                bytes.append("|");

                                bytes.append("LAC:");
                                bytes.append(cellIdentityCdma.getNetworkId());
                                bytes.append("|");
                                baseInfoList.add(bytes.toString());
                            }else if(cellInfo instanceof CellInfoWcdma){
                                CellInfoWcdma cellInfoWcdma = (CellInfoWcdma)cellInfo;
                                CellIdentityWcdma cellIdentityWcdma = cellInfoWcdma.getCellIdentity();

                                bytes.append("MCC");
                                bytes.append(cellIdentityWcdma.getMcc());
                                bytes.append("|");

                                bytes.append("MNC:");
                                bytes.append(cellIdentityWcdma.getMnc());
                                bytes.append("|");

                                bytes.append("CID:");
                                bytes.append(cellIdentityWcdma.getCid());
                                bytes.append("|");

                                bytes.append("LAC:");
                                bytes.append(cellIdentityWcdma.getLac());
                                bytes.append("|");
                                baseInfoList.add(bytes.toString());
                            }else if(cellInfo instanceof CellInfoLte){
                                CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
                                CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();

                                bytes.append("MCC:");
                                bytes.append(cellIdentityLte.getMcc());
                                bytes.append("|");

                                bytes.append("MNC:");
                                bytes.append(cellIdentityLte.getMnc());
                                bytes.append("|");

                                bytes.append("CID:");
                                bytes.append(cellIdentityLte.getCi());
                                bytes.append("|");

                                bytes.append("LAC:");
                                bytes.append(cellIdentityLte.getTac());
                                bytes.append("|");
                                baseInfoList.add(bytes.toString());
                            }
                        }
                    }
                    mSPUtil.putList(mContext,BASEINFO, baseInfoList);
                }
            }else{

            }
        }else{

        }
    }

    public static List<String> getAllBaseModeInfo(Context context){
        List<CellInfo> infoLists;
        List<String> baseInfoList = null;
        StringBuilder bytes = new StringBuilder();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if(telephonyManager != null && telephonyManager.getSimState() == SIM_STATE_READY){
            Log.d("getAllBaseModeInfo","telephonyManager != null");
            infoLists = telephonyManager.getAllCellInfo();
            if(infoLists != null && infoLists.size() > 0){
                baseInfoList = new ArrayList<>();
                Log.d("getAllBaseModeInfo", "infoLists.size: " + infoLists.size());
              for (int i = 0 ; i < infoLists.size() ; i++){
                  CellInfo cellInfo = infoLists.get(i);
                  if( cellInfo instanceof CellInfoGsm){
                    CellInfoGsm cellInfoGsm = (CellInfoGsm)cellInfo;
                    CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();

                    bytes.append("MCC:");
                    bytes.append(cellIdentityGsm.getMcc());
                    bytes.append("|");

                    bytes.append("MNC:");
                    bytes.append(cellIdentityGsm.getMnc());
                    bytes.append("|");

                    bytes.append("CID:");
                    bytes.append(cellIdentityGsm.getCid());
                    bytes.append("|");

                    bytes.append("LAC:");
                    bytes.append(cellIdentityGsm.getLac());
                    bytes.append("|");
                    baseInfoList.add(bytes.toString());
                  }else if(cellInfo instanceof CellInfoCdma){  //CMDA电信
                    CellInfoCdma cellInfoCdma = (CellInfoCdma)cellInfo;
                    CellIdentityCdma cellIdentityCdma= cellInfoCdma.getCellIdentity();

                    bytes.append("MCC:");
                    bytes.append(telephonyManager.getNetworkOperator().substring(0, 3));
                    bytes.append("|");

                    bytes.append("MNC:");
                    bytes.append(cellIdentityCdma.getSystemId());
                    bytes.append("|");

                    bytes.append("CID:");
                    bytes.append(cellIdentityCdma.getBasestationId());
                    bytes.append("|");

                    bytes.append("LAC:");
                    bytes.append(cellIdentityCdma.getNetworkId());
                    bytes.append("|");
                    baseInfoList.add(bytes.toString());
                  }else if(cellInfo instanceof CellInfoWcdma){
                    CellInfoWcdma cellInfoWcdma = (CellInfoWcdma)cellInfo;
                    CellIdentityWcdma cellIdentityWcdma = cellInfoWcdma.getCellIdentity();

                    bytes.append("MCC");
                    bytes.append(cellIdentityWcdma.getMcc());
                    bytes.append("|");

                    bytes.append("MNC:");
                    bytes.append(cellIdentityWcdma.getMnc());
                    bytes.append("|");

                    bytes.append("CID:");
                    bytes.append(cellIdentityWcdma.getCid());
                    bytes.append("|");

                    bytes.append("LAC:");
                    bytes.append(cellIdentityWcdma.getLac());
                    bytes.append("|");
                    baseInfoList.add(bytes.toString());
                  }else if(cellInfo instanceof CellInfoLte){
                    CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
                    CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();

                    bytes.append("MCC:");
                    bytes.append(cellIdentityLte.getMcc());
                    bytes.append("|");

                    bytes.append("MNC:");
                    bytes.append(cellIdentityLte.getMnc());
                    bytes.append("|");

                    bytes.append("CID:");
                    bytes.append(cellIdentityLte.getCi());
                    bytes.append("|");

                    bytes.append("LAC:");
                    bytes.append(cellIdentityLte.getTac());
                    bytes.append("|");
                    baseInfoList.add(bytes.toString());
                  }
              }
            }
        }
        return baseInfoList;
    }

    /*public static boolean  checkBaseInfo(Context context){
        mLog.debug("checkBaseInfo");
        boolean bisOk = false;
        List<String> nowBaseInfoList = getAllBaseModeInfo(context);
        mSPUtil = SPUtil.getInstance(context,BASEINFO);
        if(mSPUtil == null){
            mLog.debug("mSPUtil == null");
            return false;
        }
        List<String> spBaseInfoList = mSPUtil.getList(context,BASEINFO);
        if( spBaseInfoList.size() == 0 || 0 == nowBaseInfoList.size() ){
            mLog.debug("size == 0");
            return false;
        }
        mLog.debug("spBaseInfoList {}",spBaseInfoList.size());
         for (int i = 0 ; i < nowBaseInfoList.size(); i++){
             mLog.debug("i  {}",i );
            for ( String list: spBaseInfoList){
                if(nowBaseInfoList.get(i).equals(list)){
                    mLog.debug("bisOk" );
                    bisOk = true;
                    break;
                }
            }
             if(bisOk)
                 break;
        }
        return bisOk;
    }*/

    private static boolean fileIsExists(String strFile) {
        try {
            File f = new File("/data/data/com.nexgo.jhyzp"+"/shared_prefs",strFile+".xml");
            if (!f.exists()) {
                return false;
            }

        } catch (Exception e) {
            return false;
        }

        return true;
    }
    public static boolean  checkBaseInfo(Context context){
        mLog.debug("checkBaseInfo");
        boolean bisOk = false;

        bisOk = fileIsExists(BASEINFO);
        if(!bisOk){
            mLog.debug("文件不存在");
            return false;
        }

        bisOk = false;
        List<String> nowBaseInfoList = getAllBaseModeInfo(context);

        mSPUtil = SPUtil.getInstance(context,BASEINFO);
        if(mSPUtil == null || nowBaseInfoList == null){
            mLog.debug("mSPUtil == null");
            return false;
        }
        mLog.debug("checkBaseInfo 22222");
        List<String> spBaseInfoList = mSPUtil.getList(context,BASEINFO);
        if( spBaseInfoList.size() == 0 || 0 == nowBaseInfoList.size()){
            mLog.debug("size == 0");
            return false;
        }
        mLog.debug("spBaseInfoList {}",spBaseInfoList.size());
        for (int i = 0 ; i < nowBaseInfoList.size(); i++){
            mLog.debug("i  {}",i );
            for ( String list: spBaseInfoList){
                if(nowBaseInfoList.get(i).equals(list)){
                    mLog.debug("bisOk1111111111");
                    bisOk = true;
                    break;
                }
            }
            if(bisOk)
                break;
        }
        return bisOk;
    }

}
